package me.ywy.fmod.fmodtest;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.fmod.FMOD;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button btnNormal;
    private Button btnLuoli;
    private Button btnDashu;
    private Button btnJingsong;
    private Button btnGaoguai;
    private Button btnKongling;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        requirePermission();
        // Example of a call to a native method

        FMOD.init(this);
    }

    private void requirePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] perms = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"};
            List<String> permissionStrs = new ArrayList<>();

            int hasRecordPermission = ContextCompat.checkSelfPermission(MainActivity.this, perms[0]);
            int hasWriteSdcardPermission = ContextCompat.checkSelfPermission(MainActivity.this, perms[1]);

            if (hasWriteSdcardPermission!=PackageManager.PERMISSION_GRANTED||hasRecordPermission!=PackageManager.PERMISSION_GRANTED) {
                requestPermissions(perms, 1000);
            }

        }
    }


    public void mFix(View view) {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Timvoice.mp3";
        switch (view.getId()) {
            case R.id.btn_normal:
                VoiceFix.fix(path, VoiceFix.MODE_NORMAL);
                break;
            case R.id.btn_luoli:
                VoiceFix.fix(path, VoiceFix.MODE_LUOLI);
                break;
            case R.id.btn_dashu:
                VoiceFix.fix(path, VoiceFix.MODE_DASHU);
                break;
            case R.id.btn_jingsong:
                VoiceFix.fix(path, VoiceFix.MODE_JINGSONG);
                break;
            case R.id.btn_gaoguai:
                VoiceFix.fix(path, VoiceFix.MODE_GAOGUAI);
                break;
            case R.id.btn_kongling:
                VoiceFix.fix(path, VoiceFix.MODE_KONGLING);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        FMOD.close();
        super.onDestroy();
    }

    private void initView() {
        btnNormal = (Button) findViewById(R.id.btn_normal);
        btnLuoli = (Button) findViewById(R.id.btn_luoli);
        btnDashu = (Button) findViewById(R.id.btn_dashu);
        btnJingsong = (Button) findViewById(R.id.btn_jingsong);
        btnGaoguai = (Button) findViewById(R.id.btn_gaoguai);
        btnKongling = (Button) findViewById(R.id.btn_kongling);

        btnNormal.setOnClickListener(this);
        btnLuoli.setOnClickListener(this);
        btnDashu.setOnClickListener(this);
        btnJingsong.setOnClickListener(this);
        btnGaoguai.setOnClickListener(this);
        btnKongling.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mFix(v);
    }
}
